<?php
/**
 * Created by PhpStorm.
 * Author: zhugege
 * Date: 2017/03/08
 * Time: 11:21
 */
namespace App\Http\Admin\Personnel\Models;

use Framework\BaseClass\Http\Admin\Model;
use App\Repositories\Train\TrainRepository;
use App\Repositories\Train\TrainQuestionRepository;
use App\Eloquent\Train\TrainAnswer;

class TrainQuestion extends Model
{

    /**
     * 获取题目列表
     * @author zhugege
     */
    public function getTrainQuestionList($pageCurrent, $pageSize)
    {
        $train = new TrainQuestionRepository();
        $data = $train->getPagingList($pageCurrent, $pageSize);
        return $data;
    }

    /**
     * 添加问题
     * @author zhugege
     * @param $question
     * @param $type
     * @param $score
     * @return mixed
     */
    public function addTrainQuestion($question, $type, $score)
    {
        $trainQuestion = new TrainQuestionRepository();
        $model = $trainQuestion->create([
            'question'  => $question,
            'type'      => $type,
            'score'     => $score,
        ]);
        return $model->id;
    }

    /**
     * 编辑问题
     * @author zhugege
     * @param $id
     * @param $question
     * @param $score
     */
    public function editTrainQuestion($id,$question, $score)
    {
        $trainQuestion = new TrainQuestionRepository();
        $trainQuestion->update($id,[
            'question'  => $question,
            'score'     => $score,
        ]);
    }

    /**
     * 删除问题
     * @author zhugege
     * @param $id
     * @return bool
     */
    public function delTrainQuestion($id)
    {
        $trainQuestion = new TrainQuestionRepository();
        $affectedRows = $trainQuestion->find($id)->delete();
        return $affectedRows;
    }

    /**
     * 获取问题详情
     * @author zhugege
     * @param $id
     * @return mixed
     */
    public function getTrainQuestionInfo($id)
    {
        $trainQuestion = new TrainQuestionRepository();
        $trainQuestionInfo = $trainQuestion->find($id);
        return $trainQuestionInfo;
    }



    /**
     * 添加答案
     * @author zhugege
     * @param $train_question_id
     * @param $type
     * @param $answerArr
     * @param $correctAnswerArr
     * @param $decideAnswer
     * @return bool
     */
    public function addTrainAnswer($train_question_id, $type, $answerArr, $correctAnswerArr, $decideAnswer)
    {
//        debug($answerArr);
        switch ($type) {
            case 1:
                // 单选题
                $insertData = [];
                foreach ($answerArr as $key => $answer) {
                    $insertData[] = [
                        'train_question_id' => $train_question_id,
                        'answer'            => $answer,
                        'is_correct_answer' => $key == $correctAnswerArr[0] ? 1 : 0,
                        'created_at'        => time(),
                        'updated_at'        => time(),
                    ];
                }
                TrainAnswer::insert($insertData);
                break;
            case 2:
                // 多选题
                $insertData = [];
                foreach ($answerArr as $key => $answer) {
                    $insertData[] = [
                        'train_question_id' => $train_question_id,
                        'answer'            => $answer,
                        'is_correct_answer' => in_array($key, $correctAnswerArr) ? 1 : 0,
                        'created_at'        => time(),
                        'updated_at'        => time(),
                    ];
                }
                TrainAnswer::insert($insertData);
                break;
            case 3:
                // 判断题
//                $decideAnswer
                $insertData = [];

                $insertData[] = [
                    'train_question_id' => $train_question_id,
                    'answer'            => '是',
                    'is_correct_answer' => $decideAnswer == 1 ? 1 : 0,
                    'created_at'        => time(),
                    'updated_at'        => time(),
                ];
                $insertData[] = [
                    'train_question_id' => $train_question_id,
                    'answer'            => '否',
                    'is_correct_answer' => $decideAnswer == 1 ? 0 : 1,
                    'created_at'        => time(),
                    'updated_at'        => time(),
                ];
                TrainAnswer::insert($insertData);
                break;
            case 4:
                // 简答题  没有答案
                break;
        }

        return true;
    }

    /**
     * 编辑答案
     * @author zhugege
     * @param $train_question_id
     * @param $type
     * @param $answerArr
     * @param $correctAnswerArr
     * @param $decideAnswer
     * @return bool
     */
    public function editTrainAnswer($train_question_id, $type, $answerArr, $correctAnswerArr, $decideAnswer)
    {
        switch ($type) {
            case 1:
                // 单选题
                $insertData = [];
                foreach ($answerArr as $key => $answer) {
                    $insertData[] = [
                        'train_question_id' => $train_question_id,
                        'answer'            => $answer,
                        'is_correct_answer' => $key == $correctAnswerArr[0] ? 1 : 0,
                        'created_at'        => time(),
                        'updated_at'        => time(),
                    ];
                }
                TrainAnswer::insert($insertData);
                break;
            case 2:
                // 多选题
                $insertData = [];
                foreach ($answerArr as $key => $answer) {
                    $insertData[] = [
                        'train_question_id' => $train_question_id,
                        'answer'            => $answer,
                        'is_correct_answer' => in_array($key, $correctAnswerArr) ? 1 : 0,
                        'created_at'        => time(),
                        'updated_at'        => time(),
                    ];
                }
                TrainAnswer::insert($insertData);
                break;
            case 3:
                // 判断题
//                $decideAnswer
                $insertData = [];

                $insertData[] = [
                    'train_question_id' => $train_question_id,
                    'answer'            => '是',
                    'is_correct_answer' => $decideAnswer == 1 ? 1 : 0,
                    'created_at'        => time(),
                    'updated_at'        => time(),
                ];
                $insertData[] = [
                    'train_question_id' => $train_question_id,
                    'answer'            => '否',
                    'is_correct_answer' => $decideAnswer == 1 ? 0 : 1,
                    'created_at'        => time(),
                    'updated_at'        => time(),
                ];
                TrainAnswer::insert($insertData);
                break;
            case 4:
                // 简答题  没有答案
                break;
        }

        return true;
    }
    /**
     * 删除答案
     * @author zhugege
     * @param $train_question_id
     * @return bool|null
     * @throws \Exception
     */

    public function delTrainAnswer($train_question_id)
    {
        $trainAnswer = new TrainAnswer;
        $affectedRows = $trainAnswer->where('train_question_id',$train_question_id)->delete();
        return $affectedRows;
    }

    /**
     * 获取answer列表
     * @author zhugege
     * @param $train_question_id
     * @return \Illuminate\Database\Eloquent\Collection|static[]
     */
    public function getTrainAnswerList($train_question_id){
        $trainAnswer = new TrainAnswer();
        $trainAnswerList = $trainAnswer->where('train_question_id',$train_question_id)->get();
        return $trainAnswerList;
    }
}